package cn.school.job.workflow;

import cn.hutool.core.date.DateUtil;
import cn.school.sys.dao.SysUserDao;
import cn.school.sys.entity.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 离职人员处理
 */
@Slf4j
public class QuitUserJob extends QuartzJobBean {

    @Resource
    SysUserDao sysUserDao;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("离职账号回收任务开始执行：" + Calendar.getInstance().getTime().getTime());
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();

        wrapper.isNotNull(SysUser::getSysuLeavedate).or().ne(SysUser::getSysuLeavedate,"");
        wrapper.eq(SysUser::getStatus,"1");
        List<SysUser> userList = sysUserDao.selectList(wrapper);
        List<Long> ids = new ArrayList<>();
        for(SysUser user:userList){
            if(!StringUtils.isEmpty(user.getSysuLeavedate())){
                Long quitTime = DateUtil.parse(user.getSysuLeavedate(),"yyyy-MM-dd").getTime();
                if(quitTime<=System.currentTimeMillis()){
                    ids.add(user.getId());
                }
            }

        }
        if(ids.size()>0){
            for (Long id: ids){
                SysUser user = new SysUser();
                user.setStatus("0");
                user.setId(id);
                sysUserDao.updateById(user);
            }

        }
        log.info("离职账号回收任务执行结束：" + Calendar.getInstance().getTime().getTime());
    }
}
